package com.macasa.panel.entity.impl;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import org.hibernate.annotations.ForeignKey;

import com.google.common.base.Objects;
import com.macasa.panel.common.utils.CastHelpers;
import com.macasa.panel.entity.api.IDomain;
import com.macasa.panel.entity.api.IPowerDnsServiceMaster;
import com.macasa.panel.entity.api.IPowerDnsServiceSlave;

@Entity
@Table(name = "power_dns_service_master")
@Getter
@Setter
@ToString(callSuper = true)
public class PowerDnsServiceMaster extends PowerDnsService implements IPowerDnsServiceMaster {

	/**
	 * The Constant serialVersionUID
	 */
	@Version
	private static final long			serialVersionUID	= 3906650739109716398L;

	@OneToMany(mappedBy = "master", cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = com.macasa.panel.entity.impl.PowerDnsServiceSlave.class)
	@ForeignKey(name = "fk_power_dns_service_slave_01")
	private List<IPowerDnsServiceSlave>	slaves;

	@OneToMany(mappedBy = "powerDnsServiceMaster", cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = com.macasa.panel.entity.impl.Domain.class)
	@ForeignKey(name = "fk_domain_03")
	private List<IDomain>				domains;

	@Override
	public boolean equals(final Object obj) {

		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof PowerDnsServiceMaster)) {
			return false;
		}

		final PowerDnsServiceMaster other = CastHelpers.cast(obj, PowerDnsServiceMaster.class);

		return super.equals(obj) && Objects.equal(this.slaves, other.slaves) && Objects.equal(this.domains, other.domains);
	}

	@Override
	public int hashCode() {
		return super.hashCode() + Objects.hashCode(this.slaves, this.domains);
	}
}
